﻿// PracticeTestCodeAll.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <vector>
#include <functional>  // std::bind()
#include <algorithm>
#include <string>

#include "FileOperator.h"
#include "sort.h"
#include "designPattern.h"


/// <summary>
/// 测试std::bind使用
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
int add(int a, int b) {
	return a + b;
}
void testBind() {
	auto Add = std::bind(add, std::placeholders::_1, std::placeholders::_2);
	std::cout << Add(20, 20) << std::endl;;

	std::function<int(int, int)> Add2 = add;
	std::cout << Add2(10, 50) << std::endl;
}

void increment(int& n) {
	++n;
}

class AddValue {
public:
	AddValue(int value) : extra_param(value) {}

	void operator()(int x) const {
		std::cout << x + extra_param << " ";
	}

private:
	int extra_param;
};

using Func = std::function<int(int, int)>;

/// <summary>
/// 函数指针
/// </summary>
/// <returns></returns>
int Add(int (*callback)(int a, int b)) {
	return callback(3, 8);
}

int main()
{
	std::vector<int> arr{22,6, 7, 2,1,3,5,4,8,9,10,9,-1 };
	Sort sort(arr);
	//sort.bubbleSort();
	//sort.quickSort(0, arr.size()-1);
	/*sort.heapSort();
	sort.print();*/

	//std::string s = "data.txt";
	//std::cin >> s;
	//writeToFile("data.txt");
	//ReadLinetFile(s);
	//writeToBinary("data.bin");
	//readBinaryFile("data.bin");
	//read_first_100_text_ints(s);

	//testBind();

	//std::vector<int> vec{ 1, 2, 3, 4, 5 };
	//std::for_each(vec.begin(), vec.end(), std::ref(increment));
	//for (const auto& elem : vec) {
	//	std::cout << elem << " "; // 输出: 2 3 4 5 6
	//}

	//std::vector<int> vec = { 1, 2, 3, 4, 5 };
	//int extra_param = 10;
	//// 使用函数对象
	//std::for_each(vec.begin(), vec.end(), AddValue(extra_param)); // 输出 11 12 13 14 15

	//Func func = add;
	//std::cout << func(1, 2);

	//std::cout << Add(add);

	// 简单工厂模式
	ShapeFactory shape;
	std::unique_ptr<Shape> circle = shape.createShape("Circle");
	circle->draw();


	// 工厂方法模式
	std::unique_ptr<LoggerFactory> log;
	log = std::make_unique<FileLoggerFactory>();
	auto fileLog = log->createLogger();
	std::string s= __DATE__;
	fileLog->log("fileLog...." + s);

	log = std::make_unique<ConsoleLoggerFactory>();
	auto consoleLog = log->createLogger();
	consoleLog->log("consoleLog......");


	// 抽象工厂
	std::unique_ptr<GUIFactory> factory;
	// 使用 Windows 工厂
	factory = std::make_unique<WindowsFactory>();
	auto winButton = factory->createButton();
	auto winCheckbox = factory->createCheckbox();
	winButton->render();    // 输出: Windows Button
	winCheckbox->render();  // 输出: Windows Checkbox

	// 使用 MacOS 工厂
	factory = std::make_unique<MacOSFactory>();
	auto macButton = factory->createButton();
	auto macCheckbox = factory->createCheckbox();
	macButton->render();    // 输出: MacOS Button
	macCheckbox->render();  // 输出: MacOS Checkbox

	return 0;
}



